/*
 * jQuery FlickrGallery - jQuery plug-in
 * Version 1.0.1, Released 
 *
 * Copyright (c) 2008 Steven Dugas
 * This work is licensed under a Creative Commons Attribution 3.0 Unported License.
 */
(function($) {
    $.fn.flickrGallery = function(options) {

        var defaults = {
            divId : 'slides',
            preloadImage: 'img/loading.gif',
            galleryHeight : 'auto',              // either [string] 'auto' or [integer]. If [integer] gallery will have a strict height to that number.
            userFlickr : 'false',                  // [string], 'true' or 'false'. Determines if gallery is local or flickr based.
            useFlickrLargeSize: 'false',         // [string], 'true' or 'false'. Determines whether gallery is Large or Small.
            flickrAPIKey: '',                   // [string], required for Flickr gallery.
            photosetID: '',                        // [string], required for Flickr gallery.
            per_page: 30,                       // [integer], amount of thumbnails per 'page'.
            useHoverIntent: 'false',            // [string], 'true' or 'false'. Uses HoverIntent plugin for better Hovers.
            useLightBox: 'false'                // [string], 'true' or 'false. Uses jQuery Lightbox plugin for Small Images
        };
        options = $.extend(defaults, options);
        return this.each(function() {
            obj = $(this);

            // Massive function to create the Image Gallery and register all event handlers. Must be a function to recreate gallery on Next/Prev Page.
            function makeGallery() {
                $("img.photo").scaleImage({
                    scale: 'fit'
                });

                $('#' + options.divId).slides({
                    preload: true,
                    preloadImage: options.preloadImage,
//				play: 5000,
//				pause: 2500,
                    hoverPause: true
                });
            }

            // Finally, if useFlickr is true, download images and create UL structure before creating Gallery. Else, just create Gallery.
            if (options.useFlickr == 'true') {
                var type = 'photoset';
                if (null != options.userId) {
                    type = 'publicPhotos';
                }
                obj.flickr({
                    api_key: options.flickrAPIKey,
                    type: type,
                    photoset_id: options.photosetID,
                    user_id: options.userId,
                    thumb_size: 'm',
                    size: options.size,
                    preloadImage: options.preloadImage,
                    per_page: options.per_page,
                    page: 1,
                    callback: function() {
                        makeGallery();
                    }
                });
            } else {
                makeGallery();
            }
        });
    };
})(jQuery);